﻿var CommunicationNameJson;
var NewDeliveryStateJson;
var DeliveryTypeJson;
var pageSize = 15;
$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetListByID" },
        dataType: "json",
        url: "../../Ajax/CommunicationAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                CommunicationNameJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $.ajax({
        type: "post",
        data: { action: "NewDeliveryState" },
        dataType: "json",
        url: "../../Ajax/NewDeliveryAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                NewDeliveryStateJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $.ajax({
        type: "post",
        data: { action: "GetDeliveryType" },
        dataType: "json",
        url: "../../Ajax/NewDeliveryAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                DeliveryTypeJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    PageClickFunc(1);
    $("#btnAdd").click(function () {
        Edit(null);
    })
})
PageClickFunc = function (pageclickednumber) {
    InitNewDeliveryList(pageclickednumber);
}
function InitNewDeliveryList(pageclickednumber) {
    $.ajax({
        type: "post",
        data: { action: "GetList", pageIndex: pageclickednumber, pageSize: pageSize },
        datatype: "json",
        url: "../../Ajax/NewDeliveryAjax.ashx",
        beforSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = $.parseJSON(data).json;
                var tbody = "";
                var pageCount = $.parseJSON(data).flag;
                $.each(json, function (k, v) {
                    tbody += "<tr  id='" + v.ID + "'>"
                    tbody += "<td><lable name='lblCommunicationName' id='" + v.CommunicationID + "'>";
                    tbody += v.CommunicationName; //市场名称
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblNewsTitle' >";
                    tbody += v.NewsTitle;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblNewsContent'>";
                    tbody += decodeURIComponent(v.NewsContent);
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPriority'>";
                    tbody += v.Priority;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblStartTime'>";
                    tbody += v.StartTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblEndTime'>";
                    tbody += v.EndTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblCreateTime'>";
                    tbody += v.CreateTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblState' id='" + v.State + "'>";
                    tbody += v.StateName;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
                $("#pager").pager({ pagenumber: pageclickednumber, pagecount: Math.ceil(pageCount / pageSize), buttonClickCallback: PageClickFunc });
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

}
//市场下拉框选择
function InitSelectMark(value) {
    var sel = "";
    $.each(CommunicationNameJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}
//状态下拉框选择
function InitSelectNewDeliveryState(value) {
    var sel = "";
    $.each(NewDeliveryStateJson, function (k, v) {
        if ((value == null && k == 0) || v.value == value)
            sel += "<option value='" + v.value + "' selected='selected'>" + v.name + "</option>";
        else
            sel += "<option value='" + v.value + "'>" + v.name + "</option>";
    })
    return sel;
}
//类型下拉框选择
function InitSelectDeliveryType(value) {
    var sel = "";
    $.each(DeliveryTypeJson, function (k, v) {
        if ((value == null && k == 0) || v.value == value)
            sel += "<option value='" + v.value + "' selected='selected'>" + v.name + "</option>";
        else
            sel += "<option value='" + v.value + "'>" + v.name + "</option>";
    })
    return sel;
}
function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblCommunicationName]").first();
    Edit(lbl);
}
function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    // var sortNum = $(lbl).parent().parent().find("lable[name=lblName]");
    var sortId = $(lbl).parent().parent().attr("id");
    var MarkID = $(lbl).parent().parent().find("lable[name=lblCommunicationName]").attr("id");
    var State = $(lbl).parent().parent().find("lable[name=lblState]").attr("id");
    var content = "";
    var data = new Date();
    if (lbl != null) {
        content = "<table><tr><td>市场名称：<select id='selMark' >" + InitSelectMark(MarkID) + "</select></td></tr>"
        content += "<tr><td>消息标题：<input type='text' id='lblNewsTitle' style='width:200px;float:right' value='" + $(lbl).parent().parent().find("lable[name=lblNewsTitle]").first().text() + "' /></td></tr>"
        content += "<tr><td>消息文本：<textarea  cols='50' rows='10' id='txtNewsContent' style='width:200px'>" + $(lbl).parent().parent().find("lable[name=lblNewsContent]").first().text() + "</textarea></td></tr>"
        content += "<tr><td>创建时间：<lable name='lblCreateTime'>" + data.getFullYear() + "." + (data.getMonth() + 1) + "." + data.getDate() + " " + data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds() + "</lable></td></tr>"
        content += "<tr><td>开始时间：<input type='text' id='txtStartTime' value='" + $(lbl).parent().parent().find("lable[name=lblStartTime]").first().text() + "' style='width:200px' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class=\"Wdate\"  /></td></tr>"
        content += "<tr><td>结束时间：<input type='text' id='txtEndTime' value='" + $(lbl).parent().parent().find("lable[name=lblEndTime]").first().text() + "' style='width:200px' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class=\"Wdate\"  /></td></tr>"
        content += "<tr><td>推送状态：<select id='selState' >" + InitSelectNewDeliveryState(State) + "</select></td></tr>"
        content += "<tr><td>推送类型：<select id='selType' >" + InitSelectDeliveryType() + "</select></td></tr>"
        content += "<tr><td>优先级别：<input type='text' id='lblPriority' value='" + $(lbl).parent().parent().find("lable[name=lblPriority]").first().text() + "' style='width:200px;float:right' /></td></tr></table>"
    }
    else {
        content = "<table><tr><td>市场名称：<select id='selMark' >" + InitSelectMark() + "</select></td></tr>"
        content += "<tr><td>消息标题：<input type='text' id='lblNewsTitle' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>消息文本：<textarea  cols='50' rows='10' id='txtNewsContent' style='width:200px'></textarea></td></tr>"
        content += "<tr><td>创建时间：<lable name='lblCreateTime'>" + data.getFullYear() + "." + (data.getMonth() + 1) + "." + data.getDate() + " " + data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds() + "</lable></td></tr>"
        content += "<tr><td>开始时间：<input type='text' id='txtStartTime' style='width:200px' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class=\"Wdate\"  /></td></tr>"
        content += "<tr><td>结束时间：<input type='text' id='txtEndTime' style='width:200px' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class=\"Wdate\"  /></td></tr>"
        content += "<tr><td>推送状态：<select id='selState' >" + InitSelectNewDeliveryState() + "</select></td></tr>"
        content += "<tr><td>推送类型：<select id='selType' >" + InitSelectDeliveryType() + "</select></td></tr>"
        content += "<tr><td>优先级别：<input type='text' id='lblPriority' style='width:200px;float:right' /></td></tr></table>"
    }

    $.dialog({
        title: "日志推送模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddMould(lbl, $(selMark), sortId);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}
function AddMould(contr, sortNum, sortId) {
    var Mark = $(sortNum).parent().parent().parent().find("#selMark").val();
    var MarkName = $(sortNum).parent().parent().parent().find("#selMark>option:selected").text();
    var NewsTitle = $(sortNum).parent().parent().parent().find("#lblNewsTitle").val();
    var NewsContent = $(sortNum).parent().parent().parent().find("#txtNewsContent").val();
    var StartTime = $(sortNum).parent().parent().parent().find("#txtStartTime").val();
    var EndTime = $(sortNum).parent().parent().parent().find("#txtEndTime").val();
    var CreateTime = $(sortNum).parent().parent().parent().find("lable[name=lblCreateTime]").text();
    var State = $(sortNum).parent().parent().parent().find("#selState[option:selected]").val();
    var Priority = $(sortNum).parent().parent().parent().find("#lblPriority").val();
    var DeliveryType = $(sortNum).parent().parent().parent().find("#selType").val();
    var StateName = $(sortNum).parent().parent().parent().find("#selState>option:selected").text();
    var data = contr == null ? { action: "Add", Mark: Mark, NewsTitle: NewsTitle, NewsContent: NewsContent, StartTime: StartTime, EndTime: EndTime, State: State, Priority: Priority, CreateTime: CreateTime, DeliveryType: DeliveryType} :
              { action: "Update", id: sortId, Mark: Mark, NewsTitle: NewsTitle, NewsContent: NewsContent, StartTime: StartTime, EndTime: EndTime, State: State, Priority: Priority, CreateTime: CreateTime, DeliveryType: DeliveryType };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/NewDeliveryAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {

            $.dialog.tips("更新成功！", 1);
            if (contr == null) {
                var tbody = "<tr id='" + data + "'>";
                tbody += "<td><lable name='lblCommunicationName' id='" + Mark + "'>";
                tbody += MarkName; //市场名称
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblNewsTitle'>";
                tbody += NewsTitle;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblNewsContent'>";
                tbody += NewsContent;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblPriority'>";
                tbody += Priority;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblStartTime'>";
                tbody += StartTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblEndTime'>";
                tbody += EndTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblCreateTime'>";
                tbody += CreateTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblState' id='" + State + "'>";
                tbody += StateName;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                tbody += "</td>";
                tbody += "</td>";
                tbody += "</tr>"
                $("#tb1").append(tbody);
            }
            else {
                $(contr).parent().parent().find("lable[name=lblCommunicationName]").text(MarkName);
                $(contr).parent().parent().find("lable[name=lblNewsTitle]").text(NewsTitle);
                $(contr).parent().parent().find("lable[name=lblNewsContent]").text(NewsContent);
                $(contr).parent().parent().find("lable[name=lblPriority]").text(Priority);
                $(contr).parent().parent().find("lable[name=lblStartTime]").text(StartTime);
                $(contr).parent().parent().find("lable[name=lblEndTime]").text(EndTime);
                $(contr).parent().parent().find("lable[name=lblCreateTime]").text(CreateTime);
                $(contr).parent().parent().find("lable[name=lblState]").text(StateName);
                $(contr).parent().parent().find("lable[name=lblState]").attr("id", State);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })

}
function Delete(contr) {
    var lbl = $(contr).parent().parent();
    if (window.confirm("确定删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: $(lbl).attr("id") },
            async: true,
            url: "../../Ajax/NewDeliveryAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $(lbl).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}
